/**     
 * @Title: NettyFilterRegistration.java   
 * @Package me.springboot.netty.filer   
 * @Description: TODO
 * @author weiwei 
 * @date 2017年8月25日 下午2:42:32   
 * @version V1.0     
 */
package me.springboot.netty.filer;

import java.util.Collection;
import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletException;

import me.springboot.netty.context.NettyEmbeddedContext;
import me.springboot.netty.servlet.AbstractNettyRegistration;

/**
 * @ClassName: NettyFilterRegistration
 * @Description: TODO
 * @author weiwei
 * @date 2017年8月25日 下午2:42:32
 * 
 */
public class NettyFilterRegistration extends AbstractNettyRegistration implements FilterRegistration.Dynamic {

	private volatile boolean initialised;

	private Filter filter;

	public NettyFilterRegistration(NettyEmbeddedContext context, String filterName, String className, Filter filter) {
		super(filterName, className, context);
		this.filter = filter;
	}

	public Filter getFilter() throws ServletException {
		if (!initialised) {
			synchronized (this) {
				if (!initialised) {
					if (null == filter) {
						try {
							filter = (Filter) Class.forName(getClassName()).newInstance();
						} catch (Exception e) {
							throw new ServletException(e);
						}
					}
					filter.init(this);
					initialised = true;
				}
			}
		}
		return filter;
	}

	@Override
	public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {

	}

	@Override
	public Collection<String> getServletNameMappings() {
		return null;
	}

	@Override
	public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
		NettyEmbeddedContext context = getNettyContext();
		for (String urlPattern : urlPatterns) {
			context.addFilterMapping(dispatcherTypes, isMatchAfter, urlPattern);
		}
	}

	@Override
	public Collection<String> getUrlPatternMappings() {
		return null;
	}

}
